﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.UI;
using System.IO;
using System.Web.Routing;


public static class ButtonExtensions
{
    public static MvcHtmlString CancelButton(this HtmlHelper html, string actionName, string controllerName, RouteValueDictionary routeValues)
    {
        var writer = new HtmlTextWriter(new StringWriter());
        var uniqueID = Guid.NewGuid().ToString();

        writer.AddAttribute(HtmlTextWriterAttribute.Onclick, String.Format("document.location.href=$('#{0}').attr('href')", uniqueID), false);
        writer.AddAttribute(HtmlTextWriterAttribute.Value, WeBlog.Resources.Strings.Cancel );
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "button");
        writer.RenderBeginTag(HtmlTextWriterTag.Input);
        writer.RenderEndTag();

        controllerName = String.IsNullOrWhiteSpace(controllerName) ? html.ViewContext.RouteData.Values["controller"].ToString() : controllerName;

        var link = LinkExtensions.ActionLink(html, " ", actionName, controllerName, routeValues, new { id = uniqueID });
        writer.Write(link.ToHtmlString());


        return MvcHtmlString.Create(writer.InnerWriter.ToString());
    }

    public static MvcHtmlString CancelButton(this HtmlHelper html, string actionName, RouteValueDictionary routeValues)
    {
        return CancelButton(html, actionName, null, routeValues);
    }

    public static MvcHtmlString CancelButton(this HtmlHelper html, string actionName, string controllerName)
    {
        return CancelButton(html, actionName, controllerName, null);
    }

    public static MvcHtmlString CancelButton(this HtmlHelper html, string actionName)
    {
        return CancelButton(html, actionName, null, null);
    }
}